﻿
using Cms.Core.Application.IService.UserInfo;
using Cms.Core.Application.Service.UserInfo.Dto;
using Cms.Core.AspNetMvc.Mvc;
using Cms.Core.Infrastructure.Ui;
using System.Threading.Tasks;
using Cms.Core.Infrastructure.Extension;
using Cms.Core.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Cms.Core.Infrastructure.Attributes;
using Cms.Core.Infrastructure.Runtime.Session;
using System;
using Newtonsoft.Json.Linq;
using Cms.Core.Infrastructure.Helps;
using Microsoft.AspNetCore.Authorization;
using System.Linq;
using Cms.Core.Infrastructure.Common;

namespace Cms.Core.Web.Controllers
{

    /// <summary>
    /// 用户信息接口
    /// </summary>
    [Display("用户信息")]
    public class UserInfoController : WebApiController
    {

        private readonly IUserService _userService = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userService"></param>

        public UserInfoController(IUserService userService) => _userService = userService;


        /// <summary>
        /// 异步添加或更新用户信息接口
        /// </summary>
        [Display("异步加载用户信息"), HttpPost]

        public async Task<AjaxResult> LoadUserInfoAsync([FromBody]JObject id)
        {

            var newId = id.ToObject("id")?.AsTo<Guid>();
            return (await _userService.LoadUserInfoAsync(newId)).ToAjaxResult();
        }


        /// <summary>
        /// 异步添加或更新用户信息接口
        /// </summary>
        [Display("异步添加或者更新用户信息"), HttpPost, ParameterFilter(nameof(UserDto.Password), nameof(UserDto.Icon))]
        public async Task<AjaxResult> AddOrUpdateUserInfoAsync([FromBody] UserDto input)
        {
            //input.NotNull(nameof(input));
            if (!ModelState.IsValid)
            {
                return new AjaxResult(ModelState.Values.SelectMany(o => o.Errors).Select(o => o.ErrorMessage).ToJoin(), AjaxResultType.Error);
            }
            return (await _userService.AddOrUpdateUserAsync(input)).ToAjaxResult();
        }

    

        /// <summary>
        /// 异步得到用户分页数据
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        [Display("异步得到用户分页数据"), HttpPost]
        public async Task<PageData> GetUserPageDataAsync([FromBody] PageInputDto requestData)
        {
      
            var pageData = await _userService.GetUserPageDataAsync(requestData);
            return pageData.ToPageData();
        }

        /// <summary>
        /// 异步软删除用户信息
        /// </summary>
        [Display("异步软删除用户信息"), HttpGet]


        public async Task<AjaxResult> SoftDeleteUserAsync(Guid id)
        {
           
            return (await _userService.SoftDeleteUserAsync(id)).ToAjaxResult();
          
        }
    }
}